<template>
<div class="container">
  <h1>Hello World</h1>
  <input v-model="userInput" placeholder="请输入您的信息" class="input" />
  <button @click="handlerSend" class="button">发送请求</button>
  <div class="result">{{ result }}</div>
  <div class="history">
    <h2>对话记录</h2>
    <ul>
      <li v-for="(entry, index) in conversationHistory" :key="index">
        <strong>输入:</strong> {{ entry.input }}<br>
        <strong>输出:</strong> {{ entry.output }}
      </li>
    </ul>
  </div>
</div>
</template>

<script setup>
import { ref } from 'vue';
import axios from 'axios';

const userInput = ref(''); // 用于存储用户输入
const result = ref(''); // 用于存储接口返回结果
const conversationHistory = ref([]); // 用于存储对话记录

const handlerSend = () => {
  console.log('发送请求');
  
  // 使用 axios 发送请求
  axios.post('/api/ai2', {
    message: userInput.value // 发送用户输入的消息
  }).then(res => {
    result.value = res.data; // 更新结果
    conversationHistory.value.push({ input: userInput.value, output: res.data }); // 记录对话
  }).catch(err => {
    console.log('请求错误:', err);
  });
}
</script>

<style scoped>
.container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  background-color: #f9f9f9;
}

.input {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
}

.button {
  padding: 10px 15px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: white;
  font-size: 16px;
  cursor: pointer;
}

.button:hover {
  background-color: #0056b3;
}

.result {
  margin-top: 20px;
  padding: 10px;
  border: 1px solid #007bff;
  border-radius: 4px;
  background-color: #e7f3ff;
  color: #0056b3;
}
</style>